 <?php
/**
 * Phrappe_ConfigLoader_RewriteRouter
 *
 * @copyright   Copyright (c) 2006, John D Wells Jr
 * @author      John D Wells Jr <mailto:wellsdjohn@gmail.com>
 * @category    Phrappe
 * @package     Phrappe_ConfigLoader
 * @uses        Zend_Controller_RewriteRouter
 * @uses        Phrappe_ConfigLoader_Abstract
 */

/** Zend_Controller_RewriteRouter */
require_once 'Zend/Controller/RewriteRouter.php';

/** Phrappe_ConfigLoader_Abstract */
require_once 'Phrappe/ConfigLoader/Abstract.php';

/**
 * Phrappe_ConfigLoader_RewriteRouter
 *
 * @copyright   Copyright (c) 2006 John D Wells Jr
 * @author      John D Wells Jr <mailto:wellsdjohn@gmail.com>
 * @category    Phrappe
 * @package     Phrappe_ConfigLoader
 * @uses        Zend_Controller_RewriteRouter
 * @uses        Phrappe_ConfigLoader_Abstract
 */
class Phrappe_ConfigLoader_RewriteRouter extends Phrappe_ConfigLoader_Abstract
{
    /**
     * Sets the Phrappe_ConfigLoader_RewriteRouter::$_loaded property, based on
     * a provided Zend_Config object that is filled with router information.
     *
     * Phrappe_ConfigLoader_RewriteRouter::load() requires the following
     * structure from the passed Zend_Config object (shown in XML, but same
     * structure applies to INI or array):
     *
     *  <router>
     *      <rewritebase />
     *      <routes>
     *          <routeName comment="Repeat for each route defined">
     *              <url />
     *              <controller />
     *              <action />
     *              <variables>
     *                  <variableName comment="Repeat for each variable">
     *                      <default />
     *                      <validate />
     *                  </variableName>
     *              </variables>
     *          </routeName>
     *      </routes>
     *  </router>
     * 
     * @param   Zend_Config $zendConfig
     * @return  Phrappe_ConfigLoader_RewriteRouter
     */
    public function load(Zend_Config $zendConfig)
    {
        $this->_zendConfig = $zendConfig;
        $this->_loaded = new Zend_Controller_RewriteRouter();

        $rewritebase = $this->_zendConfig->rewritebase;
        if (! is_null($rewritebase)) {
            $this->_loaded->setRewriteBase($rewritebase);
        }

        // If routes is null or not a Zend_Config object, return now
        $routes = $this->_zendConfig->routes;
        if (is_null($routes) || (! $routes instanceof Zend_Config)) {
            return $this;
        }

        // Pull out each route setting and add to $router object
        foreach ($this->_zendConfig->routes as $route => $settings) {
            // These will be populated with data and then used when adding route
            $default = array();
            $validate = array();

            // 'controller' and 'action' keys must always be set. If the values
            // are not defined in config, the return values will be null. Then
            // default values will be handled by Zend_Controller_RewriteRouter.
            $default['controller'] = $settings->controller;
            $default['action'] = $settings->action;

            // Pick up any variable information
            if (! is_null($settings->variables)) {
                // Even though this is an iteration, it will only loop once.
                // Still, it is the only way to get value of $var
                foreach ($settings->variables as $var => $val) {
                    $default[$var] = $val->default;
                    $validate[$var] = $val->validate;
                }
            }

            // Final step, add the route information to
            // Zend_Controller_RewriteRouter object held in
            // Phrappe_ConfigLoader_RewriteRouter::_loaded
            $this->_loaded
                 ->addRoute($route, $settings->url, $default, $validate);
        }

        return $this;
    }
}